package com.restart.game.service.impl;

import com.restart.game.common.base.BaseServicesImpl;
import com.restart.game.common.base.MyBaseMapper;
import com.restart.game.common.bean.HtmlBean;
import com.restart.game.common.reptile.Reptile97973;
import com.restart.game.entity.Info;
import com.restart.game.entity.InfoType;
import com.restart.game.mapper.InfoMapper;
import com.restart.game.mapper.InfoTypeMapper;
import com.restart.game.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.List;

/**
 * @author ReStartLin
 * @data 2018/12/12 19:41
 * @classDesc: 功能描述:
 */
@Service
public class InfoServiceImpl extends BaseServicesImpl<Info,Integer> implements InfoService {
    private final InfoMapper mapper;

    private final InfoTypeMapper infoTypeMapper;

    @Autowired
    public InfoServiceImpl(InfoMapper mapper, InfoTypeMapper infoTypeMapper) {
        this.mapper = mapper;
        this.infoTypeMapper = infoTypeMapper;
    }

    @Override
    public MyBaseMapper<Info> getMapper() {
        return mapper;
    }


    @Override
    public void searchGame97973(String gameName, int pageCount) {
        try {
            List<HtmlBean> htmlBeans = Reptile97973.searchAll("97973", gameName, pageCount);
            for (HtmlBean htmlBean : htmlBeans) {
                logger.info(htmlBean.toString());
            }
            logger.info("一共搜索到{}篇",htmlBeans.size());
            //保存
            InfoType infoType;
            //大类别
            Example exampleType = new Example(InfoType.class);
            exampleType.createCriteria().andEqualTo("type", gameName);
            List<InfoType> infoTypes = infoTypeMapper.selectByExample(exampleType);
            if (infoTypes == null || infoTypes.size() == 0) {
                infoType = new InfoType();
                infoType.setType(gameName);
                infoType.setCount(0);
                infoTypeMapper.insert(infoType);
            } else {
                infoType = infoTypes.get(0);
            }
            //篇章
            int number = 0;
            //时间顺序查找, 倒序存储
            for (int i = htmlBeans.size() - 1; i >= 0; i--) {
                HtmlBean htmlBean = htmlBeans.get(i);
                Example exampleInfo = new Example(Info.class);
                exampleInfo.createCriteria().andEqualTo("title", htmlBean.getTitle());
                List<Info> infos = mapper.selectByExample(exampleInfo);
                if (infos == null || infos.size() == 0) {
                    Info info = new Info();
                    info.setTitle(htmlBean.getTitle());
                    info.setTypeId(infoType.getId());
                    info.setUrl(htmlBean.getUrl());
                    info.setFlag(false);
                    mapper.insert(info);
                    number++;
                    Integer count = infoType.getCount();
                    infoType.setCount(++count);
                }
            }
            infoTypeMapper.updateByPrimaryKey(infoType);
            logger.info("本次一共新增加{}篇文章",number);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据typeid获得未记录的记录
     * @param typeId
     * @return
     */
    @Override
    public Info getInfoByTypeID(Integer typeId) {
        Example example = new Example(Info.class);
        example.createCriteria().andEqualTo("typeId", typeId).andEqualTo("flag", "0");
        List<Info> byExample = this.findByExample(example);
        if (byExample != null && byExample.size() > 0) {
            return byExample.get(0);
        }
        return null;
    }

    @Override
    public Info deleteInfoByID(Integer id) {
        Info info = this.findById(id);
        if (info.getFlag()) {
            return null;
        }
        info.setFlag(true);
        this.updateByPrimaryKey(info);
        return info;
    }

    /**
     * 获得子页信息 处理完成后返回
     * @param info
     * @return
     */
    @Override
    public Info getHtml(Info info) throws IOException {
        String html = Reptile97973.getHtml(info.getUrl());
        info.setContent(html);
        return info;
    }
}
